﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace Example3_ws3_level.View
{
    class View 
    {
        private SpriteBatch     m_spriteBatch;
        private Texture2D       m_texture;
        
        //note that the view scale is less than texture scale
        private int             m_viewScale = 32;
        private int             m_textureTileSize = 64;

        public View(GraphicsDeviceManager a_manager, ContentManager a_contentLoader)
        {
            //this is needed to draw sprites
            m_spriteBatch = new SpriteBatch(a_manager.GraphicsDevice);

            //Load content
            m_texture = a_contentLoader.Load<Texture2D>("tiles");
            
        }

        
        internal void DrawLevel(GraphicsDevice a_graphicsDevice, Model.Level a_level)
        {
            a_graphicsDevice.Clear(Microsoft.Xna.Framework.Color.Green);

            //draw all images
            m_spriteBatch.Begin();


            for (int x = 0; x < Model.Level.g_levelWidth; x++)
            {
                for (int y = 0; y < Model.Level.g_levelHeight; y++)
                {

                    DrawTile(x, y, a_level.m_tiles[x, y]);
                }
            }
            m_spriteBatch.End();
        }

        private void DrawTile(int a_x, int a_y, Model.Level.Tile a_tile)
        {
            
            //Get the source rectangle (pixels on the texture) for the tile type 
            Rectangle sourceRectangle = new Rectangle(m_textureTileSize * (int)a_tile, 0, m_textureTileSize, m_textureTileSize);

            //Destination rectangle in windows coordinates only scaling
            Rectangle destRect = new Rectangle(a_x * m_viewScale, a_y * m_viewScale, m_viewScale, m_viewScale);


            m_spriteBatch.Draw(m_texture, destRect, sourceRectangle, Color.White);
        }
    }
}
